//
//  CommentsCell.m
//  敢唆嘛!
//
//  Created by 教主 on 15/11/26.
//  Copyright © 2015年 zms. All rights reserved.
//

#import "CommentsCell.h"
#import "Device.h"

@interface CommentsCell ()
@property(nonatomic, strong)UIImageView *userHeaderImage;   // 用户头像
@property(nonatomic, strong)UILabel *userNameLabel;         // 用户名字
@property(nonatomic, strong)UILabel *userCommentsLabel;     // 用户评论内容
@property(nonatomic, strong)UILabel *userCommentsCreatedId;      // 评论发布时间
@property(nonatomic, strong)NSString *targetUserName;       // 接收目标用户
@end

@implementation CommentsCell
{
    CGFloat x;
    CGFloat y;
    CGFloat sx;
    CGFloat sy;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        x = KSWidth / 19;
        y = KSWidth / 19;
        sx = KSWidth / 25;
        sy = KSWidth / 25;
        
        self.promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (KSHeight - ((KSWidth - 64) / 19)) / 3, KSWidth, (KSWidth - 64) / 19)];
        self.promptLabel.alpha = 0.6;
        self.promptLabel.text = @"暂无评论哟~";
        self.promptLabel.hidden = YES;
        self.promptLabel.textColor = [UIColor blackColor];
        self.promptLabel.font = [UIFont systemFontOfSize:KSWidth / 20];
        self.promptLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_promptLabel];
        
        // 创建用户头像
        self.userHeaderImage = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, KSWidth / 10, KSWidth / 10)];
        self.userHeaderImage.layer.cornerRadius = KSWidth / 20;
        self.userHeaderImage.layer.masksToBounds = YES; // 添加遮罩
        [self.contentView addSubview:_userHeaderImage];
        
        // 创建用户名
        CGFloat userHeaderImageWidth = self.userHeaderImage.frame.size.width;
        self.userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(userHeaderImageWidth + x * 2, sy * 1.6, KSWidth - userHeaderImageWidth - sx * 7, KSWidth / 18)];
        self.userNameLabel.alpha = 0.8;
        self.userNameLabel.textColor = [UIColor blackColor];
        self.userNameLabel.font = [UIFont systemFontOfSize:KSWidth / 22];
        self.userNameLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_userNameLabel];
        
        // 创建评论时间
        self.userCommentsCreatedId = [[UILabel alloc] initWithFrame:CGRectMake(KSWidth - (KSWidth - userHeaderImageWidth - sx * 17) - x, y*1.2, KSWidth - userHeaderImageWidth - sx * 7, KSWidth / 19)];
        self.userCommentsCreatedId.textColor = [UIColor blackColor];
        self.userCommentsCreatedId.alpha = 0.6;
        self.userCommentsCreatedId.font = [UIFont systemFontOfSize:KSWidth / 34];
        self.userCommentsCreatedId.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_userCommentsCreatedId];
        
        // 创建评论内容
        CGFloat userNameLabelHeight = self.userNameLabel.frame.size.height;
        self.userCommentsLabel = [[UILabel alloc] initWithFrame:CGRectMake(userHeaderImageWidth + x * 2, userNameLabelHeight + sy * 2.2, KSWidth - userHeaderImageWidth - sx * 5, KSWidth / 22)];
        self.userCommentsLabel.textColor = [UIColor blackColor];
        self.userCommentsLabel.alpha = 0.8;
        self.userCommentsLabel.font = [UIFont systemFontOfSize:KSWidth / 28];
        self.userCommentsLabel.numberOfLines = 10;    // 最多10行
        [self.contentView addSubview:_userCommentsLabel];
    }
    return self;
    
}

- (void)setCommentsModel:(CommentsModel *)model
{
    [self.userHeaderImage sd_setImageWithURL:[NSURL URLWithString:model.userHeader]];
    self.userNameLabel.text = model.userName;
    self.targetUserName = model.tagetUserName;
    // 判断目标用户是否有值
    if (_targetUserName)
    {
        self.userCommentsLabel.text = [NSString stringWithFormat:@"回复 %@:%@",self.targetUserName ,model.userCommentsLabel];
    }
    else
    {
        self.userCommentsLabel.text = model.userCommentsLabel;
    }

    if (model.userCommentsCreatedAt)
    {
        self.userCommentsCreatedId.text = [[PrettyDate alloc] prettyDateWithReference:model.userCommentsCreatedAt];
    }
    
    // 创建评论内容的动态返回高度
    CGRect commentsBounds = [model.userCommentsLabel boundingRectWithSize:CGSizeMake(KSWidth - self.userHeaderImage.frame.size.width - sx * 5, (KSWidth / 22) * 10) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:KSWidth / 28] forKey:NSFontAttributeName] context:nil];

    self.userCommentsLabel.frame = CGRectMake(self.userHeaderImage.frame.size.width + x * 2, self.userNameLabel.frame.size.height + sy * 2.2, KSWidth - self.userHeaderImage.frame.size.width - sx * 5, commentsBounds.size.height);
    
}

// 动态返回cell
+ (CGFloat)heightForCell:(CommentsModel *)model
{
    CGFloat sx = KSWidth / 25;
    CGRect commentsBounds = [model.userCommentsLabel boundingRectWithSize:CGSizeMake(KSWidth - (KSWidth / 10) - sx * 5, (KSWidth / 22) * 10) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:KSWidth / 28] forKey:NSFontAttributeName] context:nil];
    
    CGFloat height = (KSWidth / 18) + sx * 3.5 + commentsBounds.size.height;
    return height;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
